package com.sky.service.impl;

import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.result.Result;
import com.sky.service.ShoppingService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingServicelmpl implements ShoppingService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 查看购物车
     *
     * @return
     */
    @Override
    public List<ShoppingCart> showShoppingCart() {
        Long userId = BaseContext.getCurrentId();
        List<ShoppingCart> list = shoppingCartMapper.list(userId);
        return list;
    }

    /**
     * 添加到购物车
     *
     * @param dto
     * @return
     */
    @Override
    public Result add(ShoppingCartDTO dto) {
        //1.根据条件查询当前商品是否在购物车中
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(dto, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        ShoppingCart queryShoppingCar = shoppingCartMapper.queryShoppingCar(shoppingCart);
        //2.如果已经存在，则更新购买的数量
        if (queryShoppingCar != null) {
            queryShoppingCar.setNumber(queryShoppingCar.getNumber() + 1);
            shoppingCartMapper.updateNumberById(queryShoppingCar);
        } else {

            //3.1 判断当前添加的是菜品还是套餐

            Long dishId = dto.getDishId();
            if (dishId != null) {//如果是菜品
                //根据菜品id查询菜品
                Dish dish = dishMapper.getByDishId(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            }
            Long setmealId = dto.getSetmealId();
            if (setmealId != null) {//如果是套餐
                //根据套餐id查询套餐
                SetmealVO setmealVO = setmealMapper.getbyId(dto.getSetmealId());
                shoppingCart.setAmount(setmealVO.getPrice());
                shoppingCart.setImage(setmealVO.getImage());
                shoppingCart.setName(setmealVO.getName());
            }

            //根据套餐id查询套餐
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());

            shoppingCartMapper.insert(shoppingCart);
        }
        return Result.success();
    }

    /**
     * 清空购物车商品
     *
     * @return
     */
    @Override
    public void deleteShopping() {
        shoppingCartMapper.delete(BaseContext.getCurrentId());
    }

    /**
     * 单个删除
     * if>1,-1.if=1 ,delete
     * @param
     * @return
     */
    @Override
    public Result sub(ShoppingCartDTO dto) {
        ShoppingCart shoppingCart=new ShoppingCart();
        BeanUtils.copyProperties(dto,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        Long dishId = dto.getDishId();
        shoppingCart.setDishId(dishId);
        ShoppingCart queryShoppingCar = shoppingCartMapper.queryShoppingCar(shoppingCart);
        Integer number = queryShoppingCar.getNumber();
        //非空判断
        if (number==null){
            Result.error(MessageConstant.UNKNOWN_ERROR);
        }
        if (number==1){
            shoppingCartMapper.deleteSub(shoppingCart.getDishId());
        }else {
            if (number>1){
                queryShoppingCar.setNumber(queryShoppingCar.getNumber() -1);
                shoppingCartMapper.updateNumberById(queryShoppingCar);
            }
        }
        return Result.success();
    }
}
